package com.luojialong.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luojialong.constant.PermissionStrategy;
import com.luojialong.constant.UserRank;
import com.luojialong.enums.AffairStatus;
import com.luojialong.exception.AffairException;
import com.luojialong.model.domain.ProjectTb;
import com.luojialong.model.request.projectTb.ProjectTbAddRequest;
import com.luojialong.model.request.projectTb.ProjectTbGetOneRequest;
import com.luojialong.model.request.projectTb.ProjectTbQueryRequest;
import com.luojialong.model.vo.projectTb.ProjectTbInfoVo;
import com.luojialong.model.vo.projectTb.ProjectTbPageVo;
import com.luojialong.resultVo.ResultResponse;
import com.luojialong.service.ProjectTbService;
import com.luojialong.utils.ResultUtils;
import com.luojialong.verification.MyCreateLog;
import com.luojialong.verification.UserPermissionCheck;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 教师比赛项目的操作
 * @author 罗家龙
 */
@RestController
@RequestMapping("/pro")
public class ProjectTbController {

    @Resource
    private ProjectTbService projectTbService;

    @PostMapping("/add")
    @UserPermissionCheck(LeastPrivilege = UserRank.STUDENT_CONTEST)
    @MyCreateLog(doMethod = "项目的信息添加")
    public ResultResponse<Long> addTeacherPro(@RequestBody ProjectTbAddRequest projectTbAddRequest, HttpServletRequest request) {
        ProjectTb addProjectTb = projectTbService.getAddProjectTb(projectTbAddRequest);
        boolean save = projectTbService.save(addProjectTb);
        if (!save) {
            throw new AffairException(AffairStatus.FAILURE,"网络问题,项目数据没有添加成功");
        }
        return ResultUtils.success(addProjectTb.getId());
    }


    @PostMapping("/search")
    @UserPermissionCheck(LeastPrivilege = UserRank.EXPERT_REVIEW,strategy = PermissionStrategy.INVADE_STRATEGY)
    @MyCreateLog(doMethod = "分页查询参赛老师的项目")
    public ResultResponse<Page<ProjectTbPageVo>> searchProjectTbByPage(@RequestBody ProjectTbQueryRequest projectTbQueryRequest, HttpServletRequest request) {
        long current = projectTbQueryRequest.getCurrent();
        long pageSize = projectTbQueryRequest.getPageSize();
        Page<ProjectTb> page = projectTbService.page(new Page<>(current, pageSize), projectTbService.getQueryWrapper(projectTbQueryRequest));
        return ResultUtils.success(projectTbService.getProjectTbVo(page));
    }


    @PostMapping("/get")
    @MyCreateLog(doMethod = "根据id查询某一个用户的项目总信息")
    @UserPermissionCheck(LeastPrivilege = UserRank.EXPERT_REVIEW,strategy = PermissionStrategy.INVADE_STRATEGY)
    public ResultResponse<ProjectTbInfoVo> getOneById(@RequestBody ProjectTbGetOneRequest projectTbGetOneRequest, HttpServletRequest request) {
        ProjectTb projectTb = projectTbService.getById(projectTbGetOneRequest.getId());
        ProjectTbInfoVo projectTbInfoVo = ProjectTbInfoVo.transferVo(projectTb);
        return ResultUtils.success(projectTbInfoVo);
    }


    @GetMapping("/del")
    @MyCreateLog(doMethod = "删除自己的项目")
    @UserPermissionCheck(LeastPrivilege = UserRank.STUDENT_CONTEST,strategy = PermissionStrategy.PRIVATE_PROJECT)
    public ResultResponse<Boolean> deleteScoreTb(@RequestParam("userId") Long id) {
        Boolean b = projectTbService.del(id);
        if (!b) {
            throw new AffairException(AffairStatus.FAILURE, "评分信息未删除成功");
        }
        return ResultUtils.success(b);
    }

}
